package com.zretc.news.dao.impl;

import java.util.List;

import com.zretc.news.dao.NewsDao;
import com.zretc.news.entity.News;
import com.zretc.news.entity.NewsType;
import com.zretc.tools.DBTools;
import com.zretc.tools.PageData;

import javafx.geometry.Dimension2DBuilder;

public class NewsDaoImpl implements NewsDao {

	@Override
	public List<News> getNews() {
		// 调用dbtools的通用查询方法 返回结果
		return (List<News>) DBTools.exQuery("select * from news", News.class, null);
	}

	@Override
	public boolean addNews(News news) {
		// TODO Auto-generated method stub
		if (news == null) {
			return false;
		}
		// 时间为当前系统时间值
		return DBTools.exUpdate("insert into news values(null,?,?,now(),?)", news.getNewstitle(), news.getNewscontent(),
				news.getTypeid()) > 0;
	}

	@Override
	public boolean getNewsByType(int typeid) {
		// TODO Auto-generated method stub
		Integer n = (Integer) DBTools.exQuery("select count(*) from news where typeid=?", Object.class, typeid);

		return n > 0;
	}

	@Override
	public PageData<News> getNewsByPage(int page, int pageSize, String keywords) {
		// TODO Auto-generated method stub
		String sql = "select news.newsid,news.newstitle,news.newscontent,news.newsdate,newstype.typeid,newstype.typename,newstype.typeremark from news inner join newstype on news.typeid=newstype.typeid where news.newstitle like ? or news.newscontent like ?";
		return DBTools.exQueryByPage(sql, News.class, page, pageSize, "%" + keywords + "%", "%" + keywords + "%");
	}

}
